<template>
  <div class="app-container">
    <Form
      v-if="showSearch"
      v-model:formData="searchInfo"
      :formItems="formItems"
    >
      <template #footer>
        <div style="text-align: left">
          <el-button size="small" icon="Search" type="primary" @click="goSearch"
            >搜索</el-button
          >
          <el-button size="small" icon="Refresh" @click="resetSearch"
            >重置</el-button
          >
        </div>
      </template>
    </Form>
    <h3>
      <el-button
        size="small"
        icon="Plus"
        type="primary"
        @click="add"
        v-hasPermi="['api:czChannel:getList']"
        >新增</el-button
      >
    </h3>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="resetSearch"
      ></right-toolbar>
    </el-row>
    <h3></h3>
    <x-table
      :propList="propList"
      :tableData="info.items"
      :isLoading="isLoading"
      :isBorder="false"
    >
      <template #username="{ row }">
        <!-- <el-tag type="success" v-if="row.user_id == 1">否</el-tag>
        <el-tag type="info" v-else>是</el-tag> -->
        <span v-if="row.username">{{ row.username }}</span>
        <span v-else>平台</span>
      </template>
      <template #status="{ row }">
        <el-tag type="success" v-if="row.status == 1">是</el-tag>
        <el-tag type="info" v-else>否</el-tag>
      </template>
      <template #callback_type="{ row }">
        <el-tag type="success" v-if="row.callback_type == 1">推送</el-tag>
        <el-tag type="info" v-else>查询</el-tag>
      </template>
      <template #is_sync="{ row }">
        <el-tag type="success" v-if="row.is_sync == 1">是</el-tag>
        <el-tag type="info" v-else>否</el-tag>
      </template>

      <template #op="{ row }">
        <el-button
          type="primary"
          link
          icon="Edit"
          @click="change(row)"
          v-hasPermi="['api:czChannel:change']"
          >编辑</el-button
        >
        <el-button
          type="primary"
          link
          icon="Delete"
          @click="del(row)"
          v-hasPermi="['api:czChannel:del']"
          >删除</el-button
        >
      </template>
    </x-table>
    <pagination
      v-show="info.total > 0"
      :total="info.total"
      v-model:page="searchInfo.pageNum"
      v-model:limit="searchInfo.pageSize"
      @pagination="getInfo"
    />
    <Dialog
      width="30%"
      :title="dialogConfig.title"
      v-model:isShowDialog="isShowDialog"
      @confirm="submit"
    >
      <el-form
        ref="formRef"
        label-position="right"
        label-width="80px"
        :model="formData"
        :rules="formDataRules"
      >
        <el-form-item label="标识" prop="channel_code">
          <el-input v-model="formData.channel_code" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="channel_name">
          <el-input v-model="formData.channel_name"></el-input>
        </el-form-item>
        <el-form-item label="回调方式" prop="callback_type">
          <el-radio-group v-model="formData.callback_type">
            <el-radio :label="1">推送</el-radio>
            <el-radio :label="0">查询</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否同步" prop="is_sync">
          <el-radio-group v-model="formData.is_sync">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="模版" prop="channel_code">
          <el-select
            v-model="formData.channel_code"
            placeholder="请选择模版"
            @change="selectChange"
          >
            <el-option
              v-for="item in templateList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 11111 {{ tempSet }} -->
        <!-- label-width="100px" -->
        <el-form-item
          :label="item.label"
          v-for="item in tempSet"
          :key="item.code"
        >
          <el-input
            v-model="item.val"
            :placeholder="item.placeholder"
          ></el-input>
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script>
import { propList, formItems, dialogConfig } from "./config";
import {
  getList,
  add,
  change,
  del,
  getAllList,
  getTempList,
} from "@/api/channel";
export default {
  name: "channelchannelListindex",
  data() {
    return {
      curItem: {},
      dialogConfig,
      showSearch: true,
      isShowDialog: false,
      isLoading: true,
      propList,
      formItems,
      info: {},
      searchInfo: {
        pageNum: 1,
        pageSize: 10,
        // created_at: this.getTimeFilterDefault(true),
      },
      formData: {},
      tempSet: [],
      formDataRules: {
        channel_code: [{ required: true, trigger: "blur", message: "请选择" }],
        channel_name: [{ required: true, trigger: "blur", message: "请输入" }],
        callback_type: [{ required: true, trigger: "blur", message: "请选择" }],
        is_sync: [{ required: true, trigger: "blur", message: "请选择" }],
        status: [{ required: true, trigger: "blur", message: "请选择" }],
        callback_type: [{ required: true, trigger: "blur", message: "请选择" }],
      },
    };
  },
  computed: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      this.getInfo();
      this.getTempList();
    },
    async getTempList() {
      const { data } = await getTempList();
      const templateList = [];

      for (let key in data) {
        templateList.push({
          code: key,
          name: data[key]["name"],
          setting: data[key]["setting"],
        });
      }
      this.templateList = templateList;
    },
    async getInfo() {
      this.isLoading = true;
      const params = { ...this.searchInfo };
      const { data } = await getList(params);
      this.info = data;
      this.isLoading = false;
    },
    resetSearch() {
      this.searchInfo = this.$options.data.call(this).searchInfo;
      //   this.searchInfo.created_at = this.getTimeFilterDefault(true);
      this.getInfo();
    },
    selectChange(val) {
      console.log(val);
      this.templateList.forEach((item) => {
        if (item.code == val) {
          this.tempSet = item.setting;
        }
      });
    },
    goSearch() {
      // 重置时间
      this.getInfo();
    },
    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          console.log(this.formData);
          const params = { ...this.formData };
          params.setting = this.tempSet;
          if (params.id) {
            await change(params);
          } else {
            await add(params);
          }
          this.getInfo();
          this.isShowDialog = false;
          this.$message.success("操作成功");
        }
      });
    },
    add() {
      this.formData = {
        callback_type: 1,
        is_sync: 0,
        status: 1,
      };

      this.isShowDialog = true;
      // this.cur
      this.tempSet = [];
    },
    change(item) {
      console.log(item);
      this.isShowDialog = true;
      this.formData = { ...item };
      this.tempSet = item.setting;
    },
    del(item) {
      console.log(item);
      this.$modal
        .confirm("是否确认删除吗？")
        .then(function () {
          return del(item.id);
        })
        .then(() => {
          this.getInfo();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
